package net.adst.ui.other;

import com.toedter.calendar.JDateChooser;
import net.adst.resources.Constants;
import net.adst.util.GBC;
import sun.net.ResourceManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 21.11.12
 * Time: 5:08
 * To change this template use File | Settings | File Templates.
 */
public class AddOurItemDialog extends JDialog {

    private JLabel dateLabel;
    private JDateChooser dateChooser;
    private JButton backButton;
    private JButton nextButton;

    private JLabel chINLabel;
    private JTextField chINField;

    private JLabel chOutLabel;
    private JTextField chOutField;

    private JLabel tecNameLabel;
    private JComboBox tecNameCombo;

    private JLabel surnameLabel;
    private JComboBox surnameCombo;

    private JLabel hoursFactsLabel;
    private JTextField hoursFactsField;

    private JLabel hoursLabel;
    private JTextField hoursField;

    private JLabel priceForHourLabel;
    private JTextField priceForHourField;

    private JLabel profitLabel;
    private JTextField profitField;

    private JLabel profitFactLabel;
    private JTextField profitFactField;

    private JLabel kindOfPaymentLabel;
    private JTextField kindOfPaymentField;

    private JLabel customerLabel;
    private JTextField customerField;

    private JLabel objectNameLabel;
    private JTextField objectNameField;

    private JLabel dtAmountLabel;
    private JTextField dtAmountField;

    private JLabel dtCostLabel;
    private JTextField dtCostField;

    private JLabel salaryInRubLabel;
    private JTextField salaryInRubField;

    private JLabel dopTratyLabel;
    private JTextField dopTratyField;

    private JLabel pOtchetLabel;
    private JTextField pOtchetField;

    private JLabel incomeLabel;
    private JTextField incomeField;

    private JLabel prorabLabel;
    private JTextField prorabField;

    private JLabel commentLabel;
    private JTextArea commentArea;

    private JButton addButton;
    private JButton cancelButton;


    public AddOurItemDialog() {
        setLayout(new GridBagLayout());
        addElements();
        addLogic();

    }

    private void addElements() {
        ImageIcon backIcon = new ImageIcon(ResourceManager.class.getResource("/images/back.png"));
        ImageIcon nextIcon = new ImageIcon(ResourceManager.class.getResource("/images/next.png"));
        backButton = new JButton(backIcon);
        nextButton = new JButton(nextIcon);
        backButton.setMargin(new Insets(3, 3, 3, 3));
        backButton.setFocusPainted(false);
        nextButton.setMargin(new Insets(3, 3, 3, 3));
        nextButton.setFocusPainted(false);

        dateLabel = new JLabel("дата:");
        dateChooser = new JDateChooser();
        dateChooser.setMaximumSize(new Dimension(90, 18));
        dateChooser.setPreferredSize(new Dimension(90, 18));
        dateChooser.setMinimumSize(new Dimension(90, 18));
        dateChooser.setCalendar(Calendar.getInstance());

        Dimension labelDimension = new Dimension(110, 20);
        Dimension fieldDimension = new Dimension(100, 20);
        chINLabel = new JLabel("счет входящий:");
        chINLabel.setMaximumSize(labelDimension);
        chINLabel.setPreferredSize(labelDimension);
        chINLabel.setMinimumSize(labelDimension);
        chINField = new JTextField();
        chINField.setMaximumSize(fieldDimension);
        chINField.setPreferredSize(fieldDimension);
        chINField.setMinimumSize(fieldDimension);

        chOutLabel = new JLabel("счет исходящий:");
        chOutLabel.setMaximumSize(labelDimension);
        chOutLabel.setPreferredSize(labelDimension);
        chOutLabel.setMinimumSize(labelDimension);
        chOutField = new JTextField();
        chOutField.setMaximumSize(fieldDimension);
        chOutField.setPreferredSize(fieldDimension);
        chOutField.setMinimumSize(fieldDimension);


        tecNameLabel = new JLabel("именование техники:");
        tecNameLabel.setMaximumSize(labelDimension);
        tecNameLabel.setPreferredSize(labelDimension);
        tecNameLabel.setMinimumSize(labelDimension);
        tecNameCombo = new JComboBox();
        tecNameCombo.setEditable(true);
        tecNameCombo.setMaximumSize(fieldDimension);
        tecNameCombo.setPreferredSize(fieldDimension);
        tecNameCombo.setMinimumSize(fieldDimension);

        surnameLabel = new JLabel("фамилия:");
        surnameLabel.setMaximumSize(labelDimension);
        surnameLabel.setPreferredSize(labelDimension);
        surnameLabel.setMinimumSize(labelDimension);
        surnameCombo = new JComboBox();
        surnameCombo.setEditable(true);
        surnameCombo.setMaximumSize(fieldDimension);
        surnameCombo.setPreferredSize(fieldDimension);
        surnameCombo.setMinimumSize(fieldDimension);

        hoursFactsLabel = new JLabel("кол-во часов(факт):");
        hoursFactsLabel.setMaximumSize(labelDimension);
        hoursFactsLabel.setPreferredSize(labelDimension);
        hoursFactsLabel.setMinimumSize(labelDimension);
        hoursFactsField = new JTextField();
        hoursFactsField.setMaximumSize(fieldDimension);
        hoursFactsField.setPreferredSize(fieldDimension);
        hoursFactsField.setMinimumSize(fieldDimension);

        hoursLabel = new JLabel("кол-во часов:");
        hoursLabel.setMaximumSize(labelDimension);
        hoursLabel.setPreferredSize(labelDimension);
        hoursLabel.setMinimumSize(labelDimension);
        hoursField = new JTextField();
        hoursField.setMaximumSize(fieldDimension);
        hoursField.setPreferredSize(fieldDimension);
        hoursField.setMinimumSize(fieldDimension);

        priceForHourLabel = new JLabel("стоимость часа:");
        priceForHourLabel.setMaximumSize(labelDimension);
        priceForHourLabel.setPreferredSize(labelDimension);
        priceForHourLabel.setMinimumSize(labelDimension);
        priceForHourField = new JTextField();
        priceForHourField.setMaximumSize(fieldDimension);
        priceForHourField.setPreferredSize(fieldDimension);
        priceForHourField.setMinimumSize(fieldDimension);

        profitLabel = new JLabel("прибыль:");
        profitLabel.setMaximumSize(labelDimension);
        profitLabel.setPreferredSize(labelDimension);
        profitLabel.setMinimumSize(labelDimension);
        profitField = new JTextField();
        profitField.setMaximumSize(fieldDimension);
        profitField.setPreferredSize(fieldDimension);
        profitField.setMinimumSize(fieldDimension);

        profitFactLabel = new JLabel("прибыль(факт):");
        profitFactLabel.setMaximumSize(labelDimension);
        profitFactLabel.setPreferredSize(labelDimension);
        profitFactLabel.setMinimumSize(labelDimension);
        profitFactField = new JTextField();
        profitFactField.setMaximumSize(fieldDimension);
        profitFactField.setPreferredSize(fieldDimension);
        profitFactField.setMinimumSize(fieldDimension);

        kindOfPaymentLabel = new JLabel("вид оплаты:");
        kindOfPaymentLabel.setMaximumSize(labelDimension);
        kindOfPaymentLabel.setPreferredSize(labelDimension);
        kindOfPaymentLabel.setMinimumSize(labelDimension);
        kindOfPaymentField = new JTextField();
        kindOfPaymentField.setMaximumSize(fieldDimension);
        kindOfPaymentField.setPreferredSize(fieldDimension);
        kindOfPaymentField.setMinimumSize(fieldDimension);

        customerLabel = new JLabel("заказчик:");
        customerLabel.setMaximumSize(labelDimension);
        customerLabel.setPreferredSize(labelDimension);
        customerLabel.setMinimumSize(labelDimension);
        customerField = new JTextField();
        customerField.setMaximumSize(fieldDimension);
        customerField.setPreferredSize(fieldDimension);
        customerField.setMinimumSize(fieldDimension);

        objectNameLabel = new JLabel("объект:");
        objectNameLabel.setMaximumSize(labelDimension);
        objectNameLabel.setPreferredSize(labelDimension);
        objectNameLabel.setMinimumSize(labelDimension);
        objectNameField = new JTextField();
        objectNameField.setMaximumSize(fieldDimension);
        objectNameField.setPreferredSize(fieldDimension);
        objectNameField.setMinimumSize(fieldDimension);

        dtAmountLabel = new JLabel("топливо(кол-во):");
        dtAmountLabel.setMaximumSize(labelDimension);
        dtAmountLabel.setPreferredSize(labelDimension);
        dtAmountLabel.setMinimumSize(labelDimension);
        dtAmountField = new JTextField();
        dtAmountField.setMaximumSize(fieldDimension);
        dtAmountField.setPreferredSize(fieldDimension);
        dtAmountField.setMinimumSize(fieldDimension);

        dtCostLabel = new JLabel("топливо(стоимость):");
        dtCostLabel.setMaximumSize(labelDimension);
        dtCostLabel.setPreferredSize(labelDimension);
        dtCostLabel.setMinimumSize(labelDimension);
        dtCostField = new JTextField();
        dtCostField.setMaximumSize(fieldDimension);
        dtCostField.setPreferredSize(fieldDimension);
        dtCostField.setMinimumSize(fieldDimension);

        salaryInRubLabel = new JLabel("зарплата:");
        salaryInRubLabel.setMaximumSize(labelDimension);
        salaryInRubLabel.setPreferredSize(labelDimension);
        salaryInRubLabel.setMinimumSize(labelDimension);
        salaryInRubField = new JTextField();
        salaryInRubField.setMaximumSize(fieldDimension);
        salaryInRubField.setPreferredSize(fieldDimension);
        salaryInRubField.setMinimumSize(fieldDimension);

        dopTratyLabel = new JLabel("допю траты:");
        dopTratyLabel.setMaximumSize(labelDimension);
        dopTratyLabel.setPreferredSize(labelDimension);
        dopTratyLabel.setMinimumSize(labelDimension);
        dopTratyField = new JTextField();
        dopTratyField.setMaximumSize(fieldDimension);
        dopTratyField.setPreferredSize(fieldDimension);
        dopTratyField.setMinimumSize(fieldDimension);

        pOtchetLabel = new JLabel("п. отчет:");
        pOtchetLabel.setMaximumSize(labelDimension);
        pOtchetLabel.setPreferredSize(labelDimension);
        pOtchetLabel.setMinimumSize(labelDimension);
        pOtchetField = new JTextField();
        pOtchetField.setMaximumSize(fieldDimension);
        pOtchetField.setPreferredSize(fieldDimension);
        pOtchetField.setMinimumSize(fieldDimension);

        incomeLabel = new JLabel("доход:");
        incomeLabel.setMaximumSize(labelDimension);
        incomeLabel.setPreferredSize(labelDimension);
        incomeLabel.setMinimumSize(labelDimension);
        incomeField = new JTextField();
        incomeField.setMaximumSize(fieldDimension);
        incomeField.setPreferredSize(fieldDimension);
        incomeField.setMinimumSize(fieldDimension);

        prorabLabel = new JLabel("прораб:");
        prorabLabel.setMaximumSize(labelDimension);
        prorabLabel.setPreferredSize(labelDimension);
        prorabLabel.setMinimumSize(labelDimension);
        prorabField = new JTextField();
        prorabField.setMaximumSize(fieldDimension);
        prorabField.setPreferredSize(fieldDimension);
        prorabField.setMinimumSize(fieldDimension);

        commentLabel = new JLabel("пометка:");
        commentLabel.setMaximumSize(labelDimension);
        commentLabel.setPreferredSize(labelDimension);
        commentLabel.setMinimumSize(labelDimension);
        commentArea = new JTextArea();
        commentArea.setLineWrap(true);
        commentArea.setFont(UIManager.getDefaults().getFont("TabbedPane.font"));
        JScrollPane commentScrollPane = new JScrollPane(commentArea);
        commentScrollPane.setMaximumSize(new Dimension(130, 55));
        commentScrollPane.setPreferredSize(new Dimension(130, 55));
        commentScrollPane.setMinimumSize(new Dimension(130, 55));


        addButton = new JButton("Добавить");
        cancelButton = new JButton("Отменить");

        add(dateLabel, new GBC(0, 0).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(backButton, new GBC(1, 0).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(dateChooser, new GBC(2, 0).setInsets(5, 0).setAnchor(GBC.NORTHWEST).setIpad(15, 0));
        add(nextButton, new GBC(3, 0).setInsets(5, 0).setAnchor(GBC.NORTHWEST));
        add(chINLabel, new GBC(0, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(chINField, new GBC(1, 1, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(chOutLabel, new GBC(0, 2).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(chOutField, new GBC(1, 2, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(tecNameLabel, new GBC(0, 3).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(tecNameCombo, new GBC(1, 3, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(surnameLabel, new GBC(0, 4).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(surnameCombo, new GBC(1, 4, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(hoursFactsLabel, new GBC(0, 5).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(hoursFactsField, new GBC(1, 5, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(hoursLabel, new GBC(0, 6).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(hoursField, new GBC(1, 6, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(priceForHourLabel, new GBC(0, 7).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(priceForHourField, new GBC(1, 7, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(profitLabel, new GBC(0, 8).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(profitField, new GBC(1, 8, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(profitFactLabel, new GBC(0, 9).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(profitFactField, new GBC(1, 9, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(kindOfPaymentLabel, new GBC(0, 10).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(kindOfPaymentField, new GBC(1, 10, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(customerLabel, new GBC(0, 11).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(customerField, new GBC(1, 11, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(objectNameLabel, new GBC(0, 12).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(objectNameField, new GBC(1, 12, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(dtAmountLabel, new GBC(0, 13).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(dtAmountField, new GBC(1, 13, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(dtCostLabel, new GBC(0, 14).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(dtCostField, new GBC(1, 14, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(salaryInRubLabel, new GBC(0, 15).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(salaryInRubField, new GBC(1, 15, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(dopTratyLabel, new GBC(0, 16).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(dopTratyField, new GBC(1, 16, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(pOtchetLabel, new GBC(0, 17).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(pOtchetField, new GBC(1, 17, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(incomeLabel, new GBC(0, 18).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(incomeField, new GBC(1, 18, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(prorabLabel, new GBC(0, 19).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(prorabField, new GBC(1, 19, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));


        add(commentLabel, new GBC(0, 20).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(commentScrollPane, new GBC(1, 20, 3, 1).setInsets(5, 5, 11, 5).setWeight(1, 1).setFill(GBC.BOTH));

        add(addButton, new GBC(0, 21).setInsets(5, 5).setAnchor(GBC.CENTER));
        add(cancelButton, new GBC(1, 21, 3, 1).setInsets(5, 5).setAnchor(GBC.CENTER));
    }

    public void init() {
        setDialogSettings();
        getContentPane().setBackground(Constants.COLOR_LIGHT_BLUE);
//        fillTextFields();
        setVisible(true);
    }

    private void setDialogSettings() {
//        setResizable(false);
        setModal(true);
        setModalityType(ModalityType.APPLICATION_MODAL);
        pack();
        setLocationRelativeTo(Frame.getFrames()[0]);
        setTitle("Добавление записи в таблицу \"Наша. тех.\"");
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

    }

    private void addLogic() {
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //ORS-65 fix
                if ((dateChooser.getDate() == null)) {
                    return;
                }
                Calendar dtFr = dateChooser.getCalendar();
                dtFr.add(Calendar.DATE, 1);
                dateChooser.setCalendar(dtFr);
            }
        });

        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //ORS-65 fix
                if ((dateChooser.getDate() == null)) {
                    return;
                }
                Calendar dtFr = dateChooser.getCalendar();
                dtFr.add(Calendar.DATE, -1);
                dateChooser.setCalendar(dtFr);
            }
        });

    }

}
